package edu.xpu.project.vehicle.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.xpu.project.vehicle.entity.BusSchedule;
import edu.xpu.project.vehicle.repository.BusScheduleRepository;


/**
 * @describe BusScheduleService的业务实现类
 * @author liukang
 * @date 2017年5月11日
 */
@Service
@Transactional(readOnly=true)
public class BusScheduleServiceImpl implements BusScheduleService {
	
	@Autowired
	private BusScheduleRepository busScheduleRepository;

	@Override
	public BusSchedule get(Long id) {
		return busScheduleRepository.getOne(id);
	}

	@Transactional(readOnly=false)
	@Override
	public BusSchedule saveOrUpdate(BusSchedule t) {
		BusSchedule saveAndFlush = busScheduleRepository.saveAndFlush(t);
		return saveAndFlush;
	}

	@Transactional(readOnly=false)
	@Override
	public void delete(Long id) {
		busScheduleRepository.delete(id);
	}

	@Override
	public Page<BusSchedule> getPageByCriterias(String pageNumberStr,
			String pageSizeStr, final BusSchedule busSchedule) {
		Integer pageNumber = 1;
		Integer pageSize = 10;
		try {
			pageNumber = Integer.parseInt(pageNumberStr);
		} catch (Exception e) {}
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (Exception e) {}
		Pageable pageable = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.ASC, "startTime"));
		return busScheduleRepository.findAll(new Specification<BusSchedule>() {
			
			@Override
			public Predicate toPredicate(Root<BusSchedule> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				//添加查询条件
				if (busSchedule == null) {
					return null;
				}
				List<Predicate> predicates = new ArrayList<Predicate>(2);
				//1、始发站
				if (busSchedule.getStartStation() != null && busSchedule.getStartStation() != -1) {
					predicates.add(cb.equal(root.get("startStation").as(Integer.class), busSchedule.getStartStation()));
				}
				//2、终点站
				if (busSchedule.getEndStation() != null && busSchedule.getEndStation() != -1) {
					predicates.add(cb.equal(root.get("endStation").as(Integer.class), busSchedule.getEndStation()));
				}
				if (predicates.size() == 0) {
					return null;
				}
				Predicate[] predArr= new Predicate[predicates.size()];
				return cb.and(predicates.toArray(predArr));
			}
		}, pageable);
	}

	@Override
	public List<BusSchedule> findByStartStationAndEndStation(
			Integer startStation, Integer endStation) {
		List<BusSchedule> findByStartStationAndEndStation = busScheduleRepository.findByStartStationAndEndStation(startStation, endStation);
		return findByStartStationAndEndStation;
	}

	@Override
	public List<BusSchedule> findAll() {
		return busScheduleRepository.findAll();
	}

}
